﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Lavka.Models;

namespace Lavka.Core
{
    public class CustomersHelper
    {
        public static Customers GetCustomerByOrderKey(string orderKey, LavkaDbEntities context)
        {
            var order = context.Orders.Where(o => o.OrderKey == orderKey);
            if (order.Count() > 0)
            {
                return order.First().Customers;
            }
            return null;
        }
        public static string GetActionPrice(Products product, LavkaDbEntities context)
        {
            if (context.Actions.Where(a => a.ProductId == product.ProductId).Count() > 0)
            {
                var productAction = context.Actions.Where(a => a.ProductId == product.ProductId).First();
                if (!string.IsNullOrEmpty(productAction.NewPrice.ToString()))
                {
                    return string.Format("{0:F}", productAction.NewPrice);
                }
            }
            return null;
        }
        public static Customers CreateCustomer(string name, string phone, string email, string deliveryAddress, LavkaDbEntities context)
        {
            Customers customer = new Customers()
            {
                Name = name,
                Phone = phone,
                Email = email,
                DeliveryAddress = deliveryAddress
            };
            context.Customers.AddObject(customer);
            context.SaveChanges();
            return customer;
        }
        public static Customers UpdateCustomer(string customerId, string name, string phone, string email, string deliveryAddress, LavkaDbEntities context)
        {
            Customers customer = new Customers()
            {
                CustomerId = Convert.ToInt32(customerId),
                Name = name,
                Phone = phone,
                Email = email,
                DeliveryAddress = deliveryAddress
            };
            context.Customers.Attach(customer);
            context.ObjectStateManager.ChangeObjectState(customer, EntityState.Modified);
            context.SaveChanges();
            return customer;
        }
    }
}